<?php

// 后台首页控制器
class IndexController extends AuthController{
	public function index(){
      // p($_SERVER);die;
		$this->display();
	}


// 欢迎页面
 public function welcome(){
     
 	$this->display();
 }

// 退出后台
 public function out(){
 	session_unset();
 	session_destroy();
 	$this->success('退出成功',__APP__.'?c=login');
 }
 
// 用户管理
// 用户列表
public function user_list(){
    $user_list=M('hd_user')->query("select * from hd_user");

  $this->assign('user_list',$user_list);
  $this->display();


}


// 锁定
public function lock(){
  $uid=(int)$_GET['id'];
  M('hd_user')->exec("update hd_user set islock=1 where uid='$uid'");
  $this->success('锁定成功',__APP__.'?a=user_list');

}

// 解锁
public function unlock(){
  $uid=(int)$_GET['id'];
  M('hd_user')->exec("update hd_user set islock=0 where uid='$uid'");
  $this->success('锁定成功',__APP__.'?a=user_list');

}



 // 删除用户
public function user_del(){
   $uid=(int)$_GET['id'];
   M('hd_user')->where("uid=$uid")->delete();
 // exec("delete from hd_user where uid='$uid' " );
   $this->success('删除成功',__APP__.'?a=user_list');

}

// 修改用户
public function user_edit(){
$uid=(int)$_GET['id'];
 
      $olddata=M('hd_user')->where("uid=$uid")->find();
      $this->assign('olddata',$olddata);


      if(IS_POST){
      	  $passwd = md5($_POST['passwd']);
          $username=$_POST['username'];
 
      	if($_POST['passwd'] !== $_POST['repasswd']){

      		$this->error('两次密码不一致');
      	}else{

      		M('hd_user')->exec("UPDATE hd_user SET username='$username',passwd='$passwd'  where uid={$uid}");
      		$this->success('修改成功',__APP__.'?a=user_list');

      	}
      }

      	
    $this->display();

  }





// 问题列表
public function ask(){
  $w=isset($_GET['w'])?(int)$_GET['w']:1;
		$this->assign('w',$w);
		switch ($w) {
			case 1:
				$sql="SELECT * FROM hd_ask";
				break;
			case 2:
				$sql="SELECT * FROM hd_ask where solve=0";
				break;
			case 3:
				$sql="SELECT * FROM hd_ask where solve=1";
				break;
			case 4:
				$sql="SELECT * FROM hd_ask where answer=0";
				break;
			
	}
		$ask=M('hd_ask')->query($sql);
		$this->assign('ask',$ask);
		$this->display();	
}
// 删除问题
public function ask_del(){
 $asid=(int)$_GET['id'];
   M('hd_ask')->where("asid=$asid")->delete();
 // exec("delete from hd_ask where asid='$asid' " );
   $this->success('删除成功',__APP__.'?a=ask');

}
// 回答管理

// 回答列表

public function answer(){
	 $q=isset($_GET['q'])?(int)$_GET['q']:1;
		$this->assign('q',$q);
		switch ($q) {
			case 1:
				$sql="SELECT * FROM hd_answer";
				break;
			case 2:
				$sql="SELECT * FROM hd_answer where accept=0";
				break;
			case 3:
				$sql="SELECT * FROM  hd_answer where accept=1";
				break;
			
			
	}
		$answer=M('hd_answer')->query($sql);
		$this->assign('answer',$answer);
		$this->display();	
}
// 回答删除
public function answer_del(){
   $anid=(int)$_GET['id'];
   M('hd_answer')->where("anid=$anid")->delete();
 $this->success('删除成功',__APP__.'?a=answer');
}


// 分类管理

// 分类列表
public function  category(){
   $cate=M('hd_category')->query("SELECT * FROM hd_category");
	$this->assign('cate',$cate);
	$this->display();
}

// 添加子分类
public function  cate_add(){
      $cid=(int)$_GET['cid'];
		if (IS_POST) {
			$title=$_POST['title'];

			M('hd_category')->exec("INSERT INTO hd_category set title='{$title}',pid={$cid}");
			$this->success('添加成功',__APP__.'?a=category');
		}
	   $this->display();
}
// 删除分类
 public function cate_del(){
          $cid=(int)$_GET['cid'];
		M('hd_category')->exec("DELETE FROM hd_category where cid={$cid}");
		$this->success('删除成功',__APP__.'?a=category');

}

// 修改分类 
public function cate_edit(){
      $cid=(int)$_GET['cid'];
   $oldcate=M('hd_category')->where("cid=$cid")->find();
      $this->assign('oldcate',$oldcate);
		if (IS_POST) {
			$title=$_POST['title'];
			M('hd_category')->exec("UPDATE hd_category set title='{$title}' WHERE cid={$cid}");
			$this->success('修改成功',__APP__.'?a=category');
		}


    $this->display();
}

// 添加顶级分类

public function topcate_add(){
if (IS_POST) {
			$title=$_POST['title'];
			M('hd_category')->exec("INSERT INTO hd_category set title='{$title}',pid=0");
			$this->success('添加成功',__APP__.'?a=topcate_add');
		}
      $this->display();

} 



/*  用户管理
*/
// 后台用户列表
public function  admin_list(){
	 $admin_list=M('hd_admin')->query("select * from hd_admin");

      $this->assign('admin_list',$admin_list);
  $this->display();
}

// 删除后台用户
public function admin_del(){

  $aid=(int)$_GET['id'];
   M('hd_admin')->where("aid=$aid")->delete();
 $this->success('删除成功',__APP__.'?a=admin_list');

}

public function admin_edit(){
   $aid=(int)$_GET['id'];
    $olddata=M('hd_admin')->where("aid=$aid")->find();
      $this->assign('olddata',$olddata);


      if(IS_POST){
      	  $passwd = md5($_POST['passwd']);
          $username=$_POST['username'];
 
      	if($_POST['passwd'] !== $_POST['repasswd']){

      		$this->error('两次密码不一致');
      	}else{

      		M('hd_user')->exec("UPDATE hd_admin SET username='$username',passwd='$passwd'  where aid={$aid}");
      		$this->success('修改成功',__APP__.'?c=admin_list');

      	}
      }

      	
    $this->display();

}

// 添加用户
public function admin_add(){
  if(IS_POST){
     
       $data=array(
         'passwd' => md5($_POST['passwd']),
          'username'=>$_POST['username'],
           'logintime'=>date('Y-m-d H:i:s')
       	);
 
      	if($_POST['passwd'] !== $_POST['repasswd']){

      		$this->error('两次密码不一致');
      	}else{

      		 M('hd_admin')->add($data);
      		$this->success('添加成功',__APP__.'?a=admin_list');

      	}
    


  }

  $this->display();
}

// 系统管理


// 修改密码
public function web_password(){

	$this->display();
}

// 网站设置
public function web_set(){
  $this->display();
}

// 奖励管理
// 金币奖励规则
public function rule_point(){
  $this->display();
}

// 经验级别规则
public function  rule_level(){
  $this->display();

}


}
?> 